
#ifndef __NODEGEOMEXPORTER_H
#define __NODEGEOMEXPORTER_H

#include <osg/Array>
#include <osg/Drawable>
#include <osg/TriangleFunctor>
//#include <carve/geom3d.hpp>
#include <osg/Vec3>

namespace NodeGeomExporter
{

	struct GetVertex
	{
		void operator() ( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3, bool ) const 
		{
			vertexList->push_back( v1 );
			vertexList->push_back( v2 );
			vertexList->push_back( v3 );
		}

		osg::Vec3Array* vertexList;
	};

	struct GetNormal
	{
		void operator() ( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3, bool ) const 
		{
			osg::Vec3 normal = ( v2 - v1 ) ^ ( v3 - v2 );
			normal.normalize();
			normalList->push_back( normal );
		}

		osg::Vec3Array* normalList;
	};

	void GetTriangles( osg::Drawable& drawable, std::vector< PT3D >& vNodePts )
	{
		osg::TriangleFunctor<GetVertex> tf;
		tf.vertexList = new osg::Vec3Array;

		drawable.accept( tf );

		osg::Vec3Array::iterator itr = tf.vertexList->begin();
		for( ; itr != tf.vertexList->end(); ++ itr )
		{
			osg::Vec3 vertex=*itr;

			//carve::geom::vector<3> polygon_normal(carve::geom::VECTOR(0, 0, 0));
			vNodePts.push_back( PT3D( vertex.x(), vertex.y(), vertex.z() ) );
		}
	}

	void GetNormals( osg::Drawable& drawable, std::vector< PT3D >& vNormals )
	{
		osg::TriangleFunctor<GetNormal> tf;
		tf.normalList = new osg::Vec3Array;

		drawable.accept( tf );

		osg::Vec3Array::iterator itr = tf.normalList->begin();
		for( ; itr != tf.normalList->end(); ++ itr )
		{
			osg::Vec3 vertex = *itr;
			vNormals.push_back( PT3D( vertex.x(), vertex.y(), vertex.z() ) );
		}
	}
}
#endif		//__NODEGEOMEXPORTER_H